import java.io.*;
import java.util.Date;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.JarOutputStream;
import java.util.zip.Deflater;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * Created with IntelliJ IDEA.
 * User: Crazyatnet
 * Date: 12-12-31
 * Time: 下午12:33
 *
 */
public class JarUtils {
    protected static void doPackageJar(File file,JarOutputStream jarOutputStream,String entryName) throws Exception {
        entryName=entryName==null||"".equals(entryName)?file.getName():entryName+"/"+file.getName();
        if (file.isDirectory()){
            for (File f:file.listFiles()){
                doPackageJar(f, jarOutputStream, entryName);
            }
        }else if (file.isFile()){
            JarEntry jarEntry =new JarEntry(entryName);
            jarOutputStream.putNextEntry(jarEntry);
            InputStream inputStream = new FileInputStream(file);
            byte[] buffer = new byte[1024];
            int len = 0 ;
            while ((len = inputStream.read(buffer)) != -1) {
                jarOutputStream.write(buffer,0,len);
            }
            inputStream.close();
            jarOutputStream.closeEntry();
        }
    }


    /**
     *
     * @param srcpath 源路径
     * @param destpath 输入jar完整路径
     * @param iscontaincurrentdir 是否将当前文件夹打入jar
     * @return
     * @throws Exception
     */
    public static boolean packageJar(String srcpath,String destpath,boolean iscontaincurrentdir) throws Exception {
        JarOutputStream jarOutputStream = new JarOutputStream(new FileOutputStream(destpath));
        jarOutputStream.setLevel(Deflater.NO_COMPRESSION);
        File srcFile = new File(srcpath);
        if(!iscontaincurrentdir&&srcFile.isDirectory()){
           for (File f:srcFile.listFiles()){
               doPackageJar(f,jarOutputStream,null);
           }
        }else {
            doPackageJar(srcFile, jarOutputStream,null);
        }
        jarOutputStream.close();
        return true;
    }

    /**
     * 默认在打jar包
     * @param srcpath
     * @return
     * @throws Exception
     */
    public static boolean packageJar(String srcpath)throws Exception{
        return packageJar(srcpath,new File(srcpath).getPath()+".jar",true);
    }

    public static void updateJar(JarFile jarFile) throws Exception {
        Enumeration<JarEntry> e = jarFile.entries();
        JarOutputStream jarOutputStream = new JarOutputStream(new FileOutputStream("R:/resources_cn.jar"));
        jarOutputStream.setLevel(Deflater.NO_COMPRESSION);
        while (e.hasMoreElements()) {
            JarEntry cjarEntry = e.nextElement();
            File file = new File("D:/project/cn4idea/src/main/resources/lib/" + cjarEntry.getName());
            InputStream inputStream =null;
            jarOutputStream.putNextEntry(new JarEntry(cjarEntry.getName()));
        if (file.exists()&&file.isFile() &&file.lastModified()>cjarEntry.getTime()) {
            inputStream = new FileInputStream(file);
        }else if (cjarEntry.isDirectory()){
            jarOutputStream.closeEntry();
            continue;
        }else {
            inputStream = jarFile.getInputStream(cjarEntry);
        }
            byte[] buffer = new byte[1024*2];
            int len = 0 ;
            while ((len = inputStream.read(buffer)) != -1) {
                jarOutputStream.write(buffer,0,len);
            }
            inputStream.close();
            jarOutputStream.closeEntry();
        }
        jarOutputStream.close();
    }
    public static void test(JarFile jarFile)throws Exception{
        Enumeration<JarEntry> e = jarFile.entries();
        int i=0;
        while (e.hasMoreElements()){
            System.out.println(e.nextElement().getName());
            i++;
        }
        System.out.println(i);
    }
    public static void compareJars(String srcpath,String destpath)throws Exception{
        JarFile srcJar = new JarFile(srcpath);
        JarFile destJar = new JarFile(destpath);
        Enumeration<JarEntry> srcE = srcJar.entries();
        Enumeration<JarEntry> destE = destJar.entries();
        boolean flag = true;
        while (srcE.hasMoreElements()||destE.hasMoreElements()) {

                JarEntry srcJarEntry = srcE.nextElement();
                //System.out.print(srcJarEntry.getName()+"-----------------");

                JarEntry destJarEntry = destE.nextElement();
                //System.out.print(destJarEntry.getName());
               if (srcJarEntry.getCrc()!=destJarEntry.getCrc())
            System.out.println(srcJarEntry.getCrc()+"-----"+srcJarEntry.getName()+"------"+destJarEntry.getCrc()+"--------"+destJarEntry.getName());
        }
    }
}
